<?php

namespace App\Http\Controllers\Web;


use App\Http\Controllers\Controller;
use App\Models\Font;
use App\Models\Information;
use App\Models\Product;
use App\Repositories\BannerRepository;
use App\Repositories\NewRepository;
use App\Repositories\QuestionRepository;
use App\Repositories\TeamRepository;
use Illuminate\Http\Request;


class IndexController extends Controller
{
    public function index(){

        $products = Product::where('status',1)->orderBy('is_index','desc')->orderBy('sort')->limit(18)->get();

        $boobs_products = Product::where('status',1)->whereIn('cup',['D','E','F','G','H'])->inRandomOrder()->limit(18)->get();

        $long_products = Product::where('status',1)->where('height','>',165)->inRandomOrder()->limit(18)->get();

        $short_products = Product::where('status',1)->where('height','<',155)->inRandomOrder()->limit(18)->get();




        $news = app(NewRepository::class)->top(6);


        $banners = app(BannerRepository::class)->getPageBanner('/');



        $teams = app(TeamRepository::class)->get();


        $questions = app(QuestionRepository::class)->get();



        return view('web.index',compact('products','news','banners','teams','boobs_products','long_products','short_products','questions'));
    }

    public function fontTest(){
        $information = Information::get();
        return view('web.font',compact('information'));
    }



    public function font(Request $request){
        $fonts = Font::where('status',1)->get();
        $ssi = '';
        $ssi_path = public_path('uploads/font/.ssi');
        if(file_exists($ssi_path)){
            $ssi = file_get_contents($ssi_path);
        }

        $css = '';
        $env = config('app.env');
        foreach ($fonts as $item){
            if($env == 'local'){
                $path = asset_upload($item->path);
                $format = 'truetype';
            }else{
                $path = asset_upload('font/'.$item->name.'.woff2?ssi='.$ssi);
                $format = 'woff2';
            }

            if($path){
                $path = trim($path);
            }

            $css .= <<<CSS
@font-face {
    font-family: '{$item->name}';
    src: url('{$path}') format($format);
    font-weight: normal;
    font-style: normal;
    font-display: swap;
}

CSS;
        }

        return response($css,200,[
            "Content-Type"=>"text/css",
        ]);


    }
}
